home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lsof_3.37 / scripts / list_fields.awk < prev    next >
Encoding:
AWK Script  |  1995-07-31  |  5.2 KB  |  187 lines

  1. # $Id: list_fields.awk,v 1.4 95/06/30 08:56:00 abe Exp $
  2. #
  3. # list_fields.awk -- sample awk script to list lsof 3.33 and above full
  4. #             field output (i.e., -F output without -0)
  5. #
  6. # NB: this is not particularly elegant awk; several sections were
  7. #     replicated, perhaps unnecessarily, to produce a sample quickly
  8. #     and simply.
  9. #
  10. #
  11. # Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
  12. # 47907.  All rights reserved.
  13. #
  14. # Written by Victor A. Abell
  15. #
  16. # This software is not subject to any license of the American Telephone
  17. # and Telegraph Company or the Regents of the University of California.
  18. #
  19. # Permission is granted to anyone to use this software for any purpose on
  20. # any computer system, and to alter it and redistribute it freely, subject
  21. # to the following restrictions:
  22. #
  23. # 1. Neither the authors nor Purdue University are responsible for any
  24. #    consequences of the use of this software.
  25. #
  26. # 2. The origin of this software must not be misrepresented, either by
  27. #    explicit claim or by omission.  Credit to the authors and Purdue
  28. #    University must appear in documentation and sources.
  29. #
  30. # 3. Altered versions must be plainly marked as such, and must not be
  31. #    misrepresented as being the original software.
  32. #
  33. # 4. This notice may not be removed or altered.
  34.  
  35. # Clear file and process status.
  36.  
  37. BEGIN {
  38.   fhdr = fdst = pidst = 0;
  39.   access = dev = devch = fd = inode = lock = name = offset = "";
  40.   proto = size = stream = type = "";
  41.   cmd = login = pgrp = pid = uid = "";
  42. }
  43.  
  44. # Start a new process.
  45.  
  46. /^p/ {
  47.   val = substr($0, 2);
  48.   if (pidst) {
  49.  
  50.   # Print a previously accumulated process set.
  51.  
  52.     printf "COMMAND       PID    PGRP  USER\n";
  53.     printf "%-9.9s  %6d  %6d", cmd, pid, pgrp;
  54.     if (login != "") { printf "  %s\n", login }
  55.     else { printf "  %s\n", uid }
  56.     pidst = 0;
  57.     cmd = login = pgrp = pid = uid = "";
  58.   }
  59.   if (fdst) {
  60.  
  61.   # Print a previously accumulated file set.
  62.  
  63.     if (fhdr == 0) {
  64.       printf "      FD   TYPE      DEVICE   SIZE/OFF      INODE  NAME\n";
  65.     }
  66.     printf "    %4.4s%1.1s%1.1s %4.4s", fd, access, lock, type;
  67.     t = dev; if (devch != "") { t = devch }
  68.     printf("  %10.10s", t);
  69.     t = size; if (offset != "") { t = offset }
  70.     printf " %10.10s", t;
  71.     t = inode; if (proto != "") { t = proto }
  72.     printf " %10.10s", t;
  73.     t = stream; if (name != "") {t = name }
  74.     printf "  %s\n", t;
  75.     access = dev = devch = fd = inode = lock = name = offset = "";
  76.     proto = size = stream = type = "";
  77.     fdst = fhdr = 0
  78.   }
  79.  
  80. # Record a new process.
  81.  
  82.   pidst = 1;
  83.   pid = val;
  84. }
  85.  
  86. /^g|^c|^u|^L/ {
  87.  
  88. # Save process set information.
  89.  
  90.   id = substr($0, 1, 1);
  91.   val = substr($0, 2);
  92.   if (id == "g") { pgrp = val; next }        # PGRP
  93.   if (id == "c") { cmd = val; next }        # command
  94.   if (id == "u") { uid = val; next }        # UID
  95.   if (id == "L") { login = val; next }        # login name
  96. }
  97.  
  98. /^f|^a|^l|^t|^d|^D|^s|^o|^i|^P|^S|^n/ {
  99.  
  100. # Save file set information.
  101.  
  102.   id = substr($0, 1, 1);
  103.   val = substr($0, 2);
  104.   if (id == "f") {
  105.     if (pidst) {
  106.  
  107.     # Print a previously accumulated process set.
  108.  
  109.       printf "COMMAND       PID    PGRP  USER\n";
  110.       printf "%-9.9s  %6d  %6d", cmd, pid, pgrp;
  111.       if (login != "") { printf "  %s\n", login }
  112.       else { printf "  %s\n", uid }
  113.       pidst = 0;
  114.       cmd = login = pgrp = pid = uid = "";
  115.     }
  116.     if (fdst) {
  117.  
  118.       # Print a previously accumulated file set.
  119.  
  120.     if (fhdr == 0) {
  121.       printf "      FD   TYPE      DEVICE   SIZE/OFF      INODE  NAME\n";
  122.     }
  123.     fhdr = 1;
  124.     printf "    %4.4s%1.1s%1.1s %4.4s", fd, access, lock, type;
  125.     t = dev; if (devch != "") { t = devch }
  126.     printf("  %10.10s", t);
  127.     t = size; if (offset != "") { t = offset }
  128.     printf " %10.10s", t;
  129.     t = inode; if (proto != "") { t = proto }
  130.     printf " %10.10s", t;
  131.     t = stream; if (name != "") {t = name }
  132.     printf "  %s\n", t;
  133.     access = dev = devch = fd = inode = lock = name = offset = "";
  134.     proto = size = stream = type = "";
  135.     }
  136.  
  137.   # Start an new file set.
  138.  
  139.     fd = val;
  140.     fdst = 1;
  141.     next;
  142.   }
  143.  
  144. # Save file set information.
  145.  
  146.   if (id == "a") { access = val; next }        # access
  147.   if (id == "l") { lock = val; next }        # lock
  148.   if (id == "t") { type = val; next }        # type
  149.   if (id == "d") { devch = val; next }        # device characters
  150.   if (id == "D") { dev = val; next }        # device major/minor numbers
  151.   if (id == "s") { size = val; next }        # size
  152.   if (id == "o") { offset = val; next }        # offset
  153.   if (id == "i") { inode = val; next }        # inode number
  154.   if (id == "P") { proto = val; next }        # protocol
  155.   if (id == "S") { stream = val; next }        # stream name
  156.   if (id == "n") { name = val; next }        # name, comment, etc.
  157. }
  158.  
  159. END {
  160.   if (pidst) {
  161.  
  162.   # Print last process set.
  163.  
  164.     printf "COMMAND       PID    PGRP  USER\n";
  165.     printf "%-9.9s  %6d  %6d", cmd, pid, pgrp;
  166.     if (login != "") { printf "  %s\n", login }
  167.     else { printf "  %s\n", uid }
  168.   }
  169.   if (fdst) {
  170.  
  171.   # Print last file set.
  172.  
  173.     if (fhdr == 0) {
  174.       printf "      FD   TYPE      DEVICE   SIZE/OFF      INODE  NAME\n";
  175.     }
  176.     printf "    %4.4s%1.1s%1.1s %4.4s", fd, access, lock, type;
  177.     t = dev; if (devch != "") { t = devch }
  178.     printf("  %10.10s", t);
  179.     t = size; if (offset != "") { t = offset }
  180.     printf " %10.10s", t;
  181.     t = inode; if (proto != "") { t = proto }
  182.     printf " %10.10s", t;
  183.     t = stream; if (name != "") {t = name }
  184.     printf "  %s\n", t;
  185.   }
  186. }
  187.